package cn.carbswang.ohos.numberpickerview;

import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.Point;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import cn.carbswang.ohos.numberpickerview.library.NumberPickerView;

/**
 * Created by carbs on 2016/7/11.
 */
public class DialogNPV extends CommonDialog implements Component.ClickedListener,
    NumberPickerView.OnScrollListener, NumberPickerView.OnValueChangeListener {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0xD001100, "picker");

    private Context mContext;
    private Button mButtonGetInfo;
    private NumberPickerView mNumberPickerView;
    private String[] mDisplayValues;
    private ToastDialog toastDialog;

    /**
     * DialogNPV
     *
     * @param context Context
     */
    public DialogNPV(Context context) {
        super(context);
        mContext = context;
        setAutoClosable(true);

        Point point = new Point();
        DisplayManager.getInstance().getDefaultDisplay(mContext).get().getSize(point);
        setSize((int) (point.getPointX() * 0.8f), ComponentContainer.LayoutConfig.MATCH_CONTENT);
        Component component = LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_dialog_npv, null, false);
        setContentCustomComponent(component);

        mNumberPickerView = (NumberPickerView) component.findComponentById(ResourceTable.Id_picker);
        mNumberPickerView.setOnScrollListener(this);
        mNumberPickerView.setOnValueChangedListener(this);
        mDisplayValues = mContext.getStringArray(ResourceTable.Strarray_test_display_2);

        mButtonGetInfo = (Button) component.findComponentById(ResourceTable.Id_button_get_info);
        mButtonGetInfo.setClickedListener(this);

        mNumberPickerView.refreshByNewDisplayedValues(mDisplayValues);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_button_get_info:
                getCurrentContent();
                break;
            default:
                break;
        }
    }

    @Override
    public void onScrollStateChange(NumberPickerView view, int scrollState) {
        HiLog.debug(TAG, "onScrollStateChange : " + scrollState);
    }

    @Override
    public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
        String[] content = picker.getDisplayedValues();
        if (content != null) {
            HiLog.debug(TAG, "onValueChange content : " + content[newVal - picker.getMinValue()]);
            toast("oldVal : " + oldVal + " newVal : " + newVal,
                mContext.getString(ResourceTable.String_picked_content_is) + content[newVal - picker.getMinValue()]);
        }
    }

    private void getCurrentContent() {
        String[] content = mNumberPickerView.getDisplayedValues();
        if (content != null) {
            toast(mContext.getString(ResourceTable.String_picked_content_is)
                + content[mNumberPickerView.getValue() - mNumberPickerView.getMinValue()], "");
        }
    }

    private void toast(String value, String content) {
        if (toastDialog != null) {
            toastDialog.remove();
            toastDialog.destroy();
            toastDialog = null;
        }
        DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(mContext)
            .parse(ResourceTable.Layout_picker_toast_layout, null, false);
        Text tvValue = (Text) toastLayout.findComponentById(ResourceTable.Id_tv_value);
        Text tvContent = (Text) toastLayout.findComponentById(ResourceTable.Id_tv_content);
        tvValue.setText(value);
        tvValue.setText(value);
        if (content.isEmpty()) {
            tvContent.setVisibility(Component.HIDE);
        } else {
            tvContent.setText(content);
        }

        toastDialog = new ToastDialog(mContext);
        toastDialog.setContentCustomComponent(toastLayout);
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM);
        toastDialog.setTransparent(true);
        toastDialog.show();
    }
}
